#include "fttabwidget.h"
#include "ftpushbutton.h"

FTTabWidget::FTTabWidget(QWidget *parent) :
    FreeTrackWidget(parent)
{
    panels = new QStackedWidget();
    back_button = new FTPushButton( tr("Go to tab index...") );
    back_button->setIcon( QIcon(":/icons/level-up.png"), ICON_SIZE);
    back_button->hide();
    connect( back_button, SIGNAL(clicked()),
             this, SLOT(indexTab()) );

    index_tab = new QWidget();

    panels->addWidget( index_tab );
    panels->setCurrentWidget( index_tab );
}

void FTTabWidget::addTab( QWidget* tab, const QString& t)
{
    FTPushButton* button = new FTPushButton(t);
    appendTab( tab, button );
}

void FTTabWidget::addTab( QWidget* tab, const QIcon& i, const QSize& s)
{
    FTPushButton* button = new FTPushButton(i,s);
    appendTab( tab, button );
}

void FTTabWidget::addTab( QWidget* tab, const QString& t, const QIcon& i, const QSize& s)
{
    FTPushButton* button = new FTPushButton(t);
    button->setIcon(i,s);
    appendTab( tab, button );
}

void FTTabWidget::appendTab( QWidget* w, FTPushButton* b)
{
    buttons.append( b );
    bindings.insert( b,w );
    panels->addWidget( w );
    connect( b, SIGNAL(clicked()),
             this, SLOT(selectTab()) );
}

void FTTabWidget::addExtra(QWidget * w )
{
    extra_widgets.append( w );
}

void FTTabWidget::indexTab()
{
    panels->setCurrentWidget( index_tab );
    back_button->hide();
    emit currentChanged();
}

void FTTabWidget::selectTab()
{
    FTPushButton* btn = dynamic_cast<FTPushButton *>(sender());
    if ( btn )
    {
        panels->setCurrentWidget( bindings.value( btn ) );
        back_button->show();
        emit currentChanged();
    }
}

QWidget* FTTabWidget::currentWidget()
{
    QWidget* curr = panels->currentWidget();
    if ( curr && ( curr != index_tab ) )
        return curr;
    return NULL;
}

void FTTabWidget::setCurrentWidget(QWidget * cur )
{
    if ( bindings.key( cur, NULL) != NULL )
    {
        panels->setCurrentWidget( cur );
        back_button->show();
    }
}

void FTTabWidget::showIndex()
{
    panels->setCurrentWidget( index_tab );
    back_button->hide();
}

QLayout* FTTabWidget::createHorizontal()
{
    QVBoxLayout* grid = new QVBoxLayout();
    grid->addWidget( panels );
    grid->addWidget( back_button );

    QVBoxLayout* index_layout = new QVBoxLayout();

    int n = 0;
    QHBoxLayout* bl;
    foreach ( FTPushButton* btn, buttons )
    {
        if ( n == 0 )
        {
            bl = new QHBoxLayout();
            bl->addWidget( btn );
            n++;
        }
        else
        {
            n = 0;
            bl->addWidget( btn );
            index_layout->addLayout( bl );
        }
    }
    if ( n == 1 )
        index_layout->addLayout( bl );

    index_layout->addStretch();
    foreach ( QWidget* w, extra_widgets )
        index_layout->addWidget( w );

    index_layout->addStretch();
    if ( index_tab->layout() )
        delete index_tab->layout();
    index_tab->setLayout( index_layout );
    return grid;
}

QLayout* FTTabWidget::createVertical()
{
    QVBoxLayout* grid = new QVBoxLayout();
    grid->addWidget( panels );
    grid->addWidget( back_button );

    QVBoxLayout* index_layout = new QVBoxLayout();
    foreach ( FTPushButton* btn, buttons )
        index_layout->addWidget( btn );
    index_layout->addStretch();
    foreach ( QWidget* w, extra_widgets )
        index_layout->addWidget( w );

    if ( index_tab->layout() )
        delete index_tab->layout();
    index_tab->setLayout( index_layout );
    return grid;
}
